package com.wens.ui.example

import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.setMargins
import androidx.core.view.setPadding
import com.wens.ui.drawable.ProgressWithDoneDrawable
import com.wens.ui.example.databinding.ActivityExampleBinding
import com.wens.ui.layout.FlowLayout
import com.wens.ui.util.FloatWindowUtil
import com.wens.ui.util.ScreenInfoUtil
import com.wens.ui.view.FullScreenProgressBar
import com.wens.ui.view.RoundImageView

class ExampleActivity : AppCompatActivity() {
    private lateinit var floatWindow: RoundImageView
    private lateinit var binding: ActivityExampleBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityExampleBinding.inflate(layoutInflater)
        setContentView(binding.root)
        FullScreenProgressBar.init(this, ProgressWithDoneDrawable(this))
        binding.gravityTop.setOnClickListener {
            binding.flowLayout.gravity =
                binding.flowLayout.gravity and Gravity.HORIZONTAL_GRAVITY_MASK or Gravity.TOP
            updateUi()
        }
        binding.gravityBottom.setOnClickListener {
            binding.flowLayout.gravity =
                binding.flowLayout.gravity and Gravity.HORIZONTAL_GRAVITY_MASK or Gravity.BOTTOM
            updateUi()
        }
        binding.gravityStart.setOnClickListener {
            binding.flowLayout.gravity =
                binding.flowLayout.gravity and Gravity.VERTICAL_GRAVITY_MASK or Gravity.START
            updateUi()
        }
        binding.gravityEnd.setOnClickListener {
            binding.flowLayout.gravity = binding.flowLayout.gravity and Gravity.VERTICAL_GRAVITY_MASK or Gravity.END
            updateUi()
        }

        binding.gravityCenterHorizontal.setOnClickListener {
            binding.flowLayout.gravity =
                binding.flowLayout.gravity and Gravity.VERTICAL_GRAVITY_MASK or Gravity.CENTER_HORIZONTAL
            updateUi()
        }
        binding.gravityCenterVertical.setOnClickListener {
            binding.flowLayout.gravity =
                binding.flowLayout.gravity and Gravity.HORIZONTAL_GRAVITY_MASK or Gravity.CENTER_VERTICAL
            updateUi()
        }
        binding.minWidth.setOnClickListener {
            binding.flowLayout.minWidth = ScreenInfoUtil.dp2px(this, 100)
            updateUi()
        }
        binding.maxWidth.setOnClickListener {
            binding.flowLayout.maxWidth = ScreenInfoUtil.dp2px(this, 200)
            updateUi()
        }

        binding.minHeight.setOnClickListener {
            binding.flowLayout.minHeight = ScreenInfoUtil.dp2px(this, 100)
            updateUi()
        }
        binding.maxHeight.setOnClickListener {
            binding.flowLayout.maxHeight = ScreenInfoUtil.dp2px(this, 200)
            updateUi()
        }

        binding.padding0.setOnClickListener {
            binding.flowLayout.setPadding(0)
            updateUi()
        }

        binding.padding10.setOnClickListener {
            binding.flowLayout.setPadding(ScreenInfoUtil.dp2px(this, 10))
            updateUi()
        }

        binding.childPadding0.setOnClickListener {
            for (i in 0 until binding.flowLayout.childCount) {
                binding.flowLayout.getChildAt(i).setPadding(0)
            }
            updateUi()
        }
        binding.childPadding10.setOnClickListener {
            for (i in 0 until binding.flowLayout.childCount) {
                binding.flowLayout.getChildAt(i).setPadding(ScreenInfoUtil.dp2px(this, 10))
            }
            updateUi()
        }

        binding.childMargin0.setOnClickListener {
            for (i in 0 until binding.flowLayout.childCount) {
                (binding.flowLayout.getChildAt(i).layoutParams as FlowLayout.LayoutParams).setMargins(0)
            }
            updateUi()
        }
        binding.childMargin10.setOnClickListener {
            for (i in 0 until binding.flowLayout.childCount) {
                (binding.flowLayout.getChildAt(i).layoutParams as FlowLayout.LayoutParams).setMargins(
                    ScreenInfoUtil.dp2px(this, 10)
                )
            }
            updateUi()
        }
        binding.randomChildWidth.setOnClickListener {
            for (i in 0 until binding.flowLayout.childCount) {
                binding.flowLayout.getChildAt(i).layoutParams.width =
                    listOf(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ScreenInfoUtil.dp2px(this, 50),
                        ScreenInfoUtil.dp2px(this, 100)
                    ).random()
            }
            updateUi()
        }

        binding.randomChildHeight.setOnClickListener {
            for (i in 0 until binding.flowLayout.childCount) {
                binding.flowLayout.getChildAt(i).layoutParams.height =
                    listOf(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ScreenInfoUtil.dp2px(this, 50),
                        ScreenInfoUtil.dp2px(this, 100)
                    ).random()
            }
            updateUi()
        }

        binding.showFullScreenDialog.setOnClickListener {
            FullScreenProgressBar.show(this, true)
        }

        binding.hideFullScreenDialog.setOnClickListener {
            FullScreenProgressBar.dismiss(this)
        }
        binding.showFullScreenDialogFromXml.setOnClickListener {
            binding.fullScreenProgressBar.show()
        }

        binding.hideFullScreenDialogFromXml.setOnClickListener {
            binding.fullScreenProgressBar.dismiss()
        }
        floatWindow = RoundImageView(this)
        floatWindow.setImageResource(R.drawable.ic_launcher_foreground)
        floatWindow.setBackgroundResource(R.color.color_accent)
        floatWindow.setOnClickListener {
            FloatWindowUtil.dismiss(floatWindow)
        }
        binding.showFloatWindowBtn.setOnClickListener {
            FloatWindowUtil.show(
                floatWindow,
                ScreenInfoUtil.dp2px(this, 80),
                ScreenInfoUtil.dp2px(this, 80)
            )
        }
    }

    private fun updateUi() {
        binding.flowLayout.requestLayout()
        binding.flowLayout.invalidate()
    }

    override fun finish() {
        super.finish()
        FloatWindowUtil.dismiss(floatWindow)
    }

    override fun onBackPressed() {
        if (binding.fullScreenProgressBar.isShown)
            binding.fullScreenProgressBar.dismiss()
        else
            super.onBackPressed()
    }
}